﻿@using ST = EmoneyWeiXinCommon.StringTools;
@{
    ViewBag.Title = "UserList";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="right">
    <input type="text" id="hidid" style="display: none" />
    <h3>平台用户管理</h3>
    <!--manage-->
    <div class="manage">
        <!--manage_top-->
        <div class="manage_top">
            <div class="manage_register">
                <p><span>登录用户名：</span><input type="text" id="loginname"></p>
                <p><span>登录密码：</span><input type="text" id="pwd"></p>
            </div>
            <div class="manage_register">
                <p><span>姓名：</span><input type="text" id="username"></p>
            </div>
            <div class="manage_register">
                <div class="manage_register_p">
                    <span>平台用户权限：</span>
                    <div id="spWeixinID"></div>
                </div>
                <div class="manage_register_p">
                    <span>微信菜单栏目权限：</span>
                    <div id="spMenu"></div>
                </div>
            </div>
            <div class="manage_register" id="divLeftMenuOperate">
                <div class="manage_register_p">
                    <span>后台菜单权限：</span>
                    <div id="spLeftMenu" class="manage_register_p_box"></div>
                </div>

                @* <div class="manage_register_p">
                    <span>操作权限：</span>

                    <div id="spOperate">
                        <div class="manage_register_p_div">
                            <input type="checkbox" name="1" value="1" id="1" />
                            <label>发布</label>
                        </div>
                        <div class="manage_register_p_div">
                            <input type="checkbox" name="2" value="2" id="2" />
                            <label>添加</label>
                        </div>
                        <div class="manage_register_p_div">
                            <input type="checkbox" name="3" value="3" id="3" />
                            <label>修改</label>
                        </div>
                        <div class="manage_register_p_div">
                            <input type="checkbox" name="4" value="4" id="4" />
                            <label>删除</label>
                        </div>
                    </div>

                </div>*@

            </div>
            <div class="manage_register">
                <p><span>绑定IP：</span><input type="text" id="ip"></p>
                <p>
                    <span>绑定客服账号：</span>
                    <span id="spCustomServer"></span>
                </p>
            </div>
            <div class="manage_register">
                <p><span>是否有效：</span><input type="checkbox" id="ckIsValid" checked="checked"></p>
            </div>
            <p class="manage_add">
                <a id="btnSave">保存</a>
                <a id="btnCancel" class="manage_add_font">取消</a>
            </p>
        </div>
        <!--manage_top end-->
        <p class="manage_line"></p>
        <!--manage_bottom-->
        <div class="manage_bottom">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <th><span>编号</span></th>
                    <th><span>登录用户名</span></th>
                    <th><span>登录密码</span></th>
                    <th><span>姓名</span></th>
                    <th><span>平台用户权限</span></th>
                    <th><span>绑定IP</span></th>
                    <th><span>绑定客服账号</span></th>
                    <th><span>有效状态</span></th>
                    <th><span>更新时间</span></th>
                    <th><span>操作</span></th>
                </tr>
                <tbody id="dvList">
                </tbody>
            </table>
            <div class="manage_page" id="manage_page">
            </div>
        </div>
        <!--manage_bottom end-->
    </div>
    <!--manage end-->

</div>
<script type="text/javascript">
    var pageIndex = 1;
    var pageSize = 10;
    var pageCount = 1;
    var totalCount = 0;
    $(document).ready(function () {
        //平台用户权限
        bindCheckBoxList("@ST.GetTrendsFile("BaseConfiguration/QueryWeixXinList")", "spWeixinID", "weixinid", "id", "title", function () { });
        //微信菜单栏目权限
        bindCheckBoxList("@ST.GetTrendsFile("BaseConfiguration/QueryMenuList")", "spMenu", "menuid", "id", "title", function () { });

        //后台菜单权限和操作权限
        bindCheckBoxList2("@ST.GetTrendsFile("BaseConfiguration/QueryLeftMenuList")", "spLeftMenu", "leftmenuid", "id", "title", function () { });
        //操作权限
        //bindCheckBoxList("@ST.GetTrendsFile("BaseConfiguration/QueryOperateList")", "spOperate", "operateid", "id", "name", function () { });

        //绑定客服列表
        bindDrpList("@ST.GetTrendsFile("BaseConfiguration/QueryCustomServerList")", "spCustomServer", "drpAccount", "id", "account", function () { });
        gotoPage();



        //保存
        $("#btnSave").click(function () {
            if (ischeck()) {
                var id = $("#hidid").val();
                var loginname = $("#loginname").val();
                var pwd = $("#pwd").val();
                var username = $("#username").val();
                var leftmenuid = getCheckBoxValue("leftmenuid");
                var weixinid = getCheckBoxValue("weixinid");
                var menuid = getCheckBoxValue("menuid");
                //var operateid = getCheckBoxValue("operateid");
                var operateid = getAllOperateidList();
                var ip = $("#ip").val();
                var account = $("#drpAccount").val();
                var isvalid = $("#ckIsValid").attr("checked") == "checked" ? "1" : "0";
                if (id > 0)
                    operate("2", id, loginname, pwd, username, leftmenuid, weixinid, menuid, operateid, ip, account, isvalid);
                else
                    operate("1", "0", loginname, pwd, username, leftmenuid, weixinid, menuid, operateid, ip, account, isvalid);
            }
        });
        //取消
        $("#btnCancel").click(function () {
            reseting();
        });
    });

    function bindCheckBoxList(url, spid, id, value, name, fn) {
        $.ajax({
            url: url,
            type: "POST",
            async: true,
            data: {},
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                datatype = eval(data);
                if (datatype) {
                    $("#" + spid).html("");
                    var d = datatype;
                    var html = '';
                    for (var i = 0; i < d.length; i++) {
                        html += '<div class="manage_register_p_div"> <input type="checkbox" name="' + id + '" value="' + d[i][value] + '" id="' + d[i]["id"] + '"/>' + '<label >' + d[i][name] + '</label></div>';
                    }

                    $("#" + spid).append(html);
                    fn("1");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }


    //选择某个后台菜单权限时显示对应的操作权限div列表
    function clickLeftMenuShowOperateListOperate() {
        $("input[name='leftmenuid']").each(function () {
            $(this).click(function () {
                var id = $(this).attr("id");
                if ($(this).attr("checked") == "checked") {
                    //隐藏没有其他操作权限div列表
                    //$("#divAllOperate").not($("#div" + id)).css("display", "none");
                    $(".manage_register_p1").css("display", "none");
                    //显示当前菜单对应的操作权限div列表
                    $("#div" + id).css("display", "");
                }
                else {
                    //隐藏当前菜单对应的操作权限列表
                    $("#div" + id).css("display", "none");
                }
            })
        });
    }


    function getOperateListHtml(id) {
        var divID = "div" + id;
        var name = "operateid" + id;
        var operateList = '<div class="manage_register_p1" id="' + divID + '" style="display:none"><span>操作权限：</span>';
        operateList += ' <div id="spOperate"><div class="manage_register_p_div"><input type="checkbox" name="' + name + '" value="1" id="1" /><label>发布</label></div>';
        operateList += ' <div class="manage_register_p_div"><input type="checkbox" name="' + name + '" value="2" id="2" /><label>添加</label></div>';
        operateList += '<div class="manage_register_p_div"><input type="checkbox" name="' + name + '" value="3" id="3" /><label>修改</label></div>';
        operateList += '<div class="manage_register_p_div"><input type="checkbox" name="' + name + '" value="4" id="4" /><label>删除</label></div></div></div>';
        return operateList;
    }

    function bindCheckBoxList2(url, spid, id, value, name, fn) {
        $.ajax({
            url: url,
            type: "POST",
            async: true,
            data: {},
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                datatype = eval(data);
                if (datatype) {
                    $("#" + spid).html("");
                    var d = datatype;
                    var html = '';
                    var operateLists = "";
                    for (var i = 0; i < d.length; i++) {
                        html += '<div class="manage_register_p_div"> <input type="checkbox" name="' + id + '" value="' + d[i][value] + '" id="' + d[i]["id"] + '"/>' + '<label >' + d[i][name] + '</label></div>';
                        //操作权限
                        operateLists += getOperateListHtml(d[i]["id"]);
                    }
                    $("#" + spid).append(html);
                    $("#divLeftMenuOperate").append(operateLists);
                    clickLeftMenuShowOperateListOperate();
                    fn("1");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }


    //获取所有菜单对应的操作id，例如:1,2,3,4;1,2;1,2,3
    function getAllOperateidList() {
        var retStr = "";
        $("input[name='leftmenuid']").each(function () {
            var str = "";
            if ($(this).attr("checked") == "checked") {
                var id = $(this).attr("id");
                var name = "operateid" + id;
                $("input[name='" + name + "']").each(function () {
                    if ($(this).attr("checked") == "checked") {
                        str += $(this).val() + ",";
                    }
                });
                retStr += str.substring(0, str.length - 1) + ";";
            }
        });
        return retStr.substring(0, retStr.length - 1);
    }

    //获取CheckBox值
    function getCheckBoxValue(name) {
        var labelids = "";
        $("input[name='" + name + "']").each(function () {
            if ($(this).attr("checked") == "checked") {
                var d = $(this).val() + ",";
                labelids += d;
            }
        });
        return labelids.trim(',');
    }

    //取消所有操作项的选中状态
    function clearAllOperateCheckBox() {
        $("input[name='leftmenuid']").each(function () {
            //if ($(this).attr("checked") == "checked") {
            var id = $(this).attr("id");
            var n = "operateid" + id;
            $("input[name='" + n + "']").each(function () {
                if ($(this).attr("checked") == "checked") {
                    $(this).attr("checked", false);
                }
            });
            // }
        });
    }

    //取消选中CheckBox
    function clearCheckBoxValue(name) {
        $("input[name='" + name + "']").each(function () {
            $(this).attr("checked", false);
        });

    }
    //选中CheckBox
    function setCheckBoxValue(arrData, name) {
        arrData = "," + arrData + ",";
        $("input[name='" + name + "']").each(function () {
            var id = $(this).attr("id");
            if (arrData.indexOf("," + id + ",") != -1) {
                $(this).attr("checked", true);
            }
            else {
                $(this).attr("checked", false);
            }
        });
    }
    //设置operatelist
    function setOperateListCheckBoxValue(arrData, name) {
        //$("input[name='" + name + "']").each(function () {
        //    var id = $(this).attr("id");
        //    if (arrData.indexOf(id) != -1) {
        //        $(this).attr("checked", true);
        //    }
        //    else {
        //        $(this).attr("checked", false);
        //    }
        //});
        //取消div里的checkbox
        clearAllOperateCheckBox();
        var j = -1;
        $("input[name='leftmenuid']").each(function () {
            if ($(this).attr("checked") == "checked") {
                j++;
                var id = $(this).attr("id");
                var nn = "operateid" + id;
                var dd = arrData.split(';');
                var data = dd[j];
                $("input[name='" + nn + "']").each(function () {
                    var ids = $(this).attr("id");
                    if (data != null && data != "" && data != undefined) {
                        var arData = data.split(",");
                        if (arData.indexOf(ids) != -1) {
                            $(this).attr("checked", true);
                        }
                        else {
                            $(this).attr("checked", false);
                        }
                    }
                    else {
                        $(this).attr("checked", false);
                    }
                });
            }
        });
    }

    function bindDrpList(url, spid, id, value, name, fn) {
        $.ajax({
            url: url,
            type: "POST",
            async: true,
            data: {},
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                datatype = eval(data);
                if (datatype) {
                    $("#" + spid).html("");
                    var d = datatype;
                    var html = '<select  id="' + id + '" >';
                    for (var i = 0; i < d.length; i++) {
                        html += ' <option value="' + d[i][value] + '">' + d[i][name] + '</option>';
                    }
                    html += '</select>';
                    $("#" + spid).append(html);
                    fn("1");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }


    function ischeck() {
        var loginname = $("#loginname").val();
        if (loginname == "") {
            alert("登录用户名不能空！");
            $("#loginname").focus();
            return false;
        }
        var pwd = $("#pwd").val();
        if (pwd == "") {
            alert("登录密码不能空，请重新输入！");
            $("#pwd").focus();
            return false;
        }
        var username = $("#username").val();
        if (username == "") {
            alert("姓名不能空，请重新输入！");
            $("#username").focus();
            return false;
        }
        return true;
    }

    var ajaxdata = {};
    function gotoPage() {
        ajaxdata["pindex"] = pageIndex;
        ajaxdata["psize"] = pageSize;
        $.ajax({
            url: "@ST.GetTrendsFile("BaseConfiguration/GetUserList")",
            type: "POST",
            async: true,
            data: ajaxdata,
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                //iddenLoading();
            },
            success: function (result) {
                if (result != "" && result != null && result != undefined) {
                    var arrData = result.split("~");
                    totalCount = arrData[0];
                    var data = arrData[1];
                    var d = eval(data);
                    var html = "";
                    for (var i = 0; i < d.length; i++) {
                        html += "<tr>";
                        html += getTxtTd(d[i]["rowid"]) + getTxtTd(d[i]["loginName"]) + getTxtTd(d[i]["loginpwd"]) + getTxtTd(d[i]["Username"]) + getTxtTd(d[i]["weixinname"]);
                        html += getTxtTd(d[i]["IPaddress"]) + getTxtTd(d[i]["csname"]) + getTxtTd(d[i]["validname"]) + getTxtTd(d[i]["updatetime"].replace(/T/g, " "));
                        html += "<td><a class=\"manage_bottom_font blue\" onclick=\"getUserByID('" + d[i]["id"] + "')\" >编辑</a>";
                        html += "<a class=\"manage_bottom_font red\" onclick=\"operate('-1','" + d[i]["id"] + "')\">删除</a></td>";
                        html += '</tr>';
                    }

                    $("#dvList").html(html);
                    PageCountHtml();
                } else {
                    $("#dvList").html("没有查询到相关数据!");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    function getTxtTd(s) {
        return '<td><span>' + s + '</span></td>';
    }
    //查询
    function getUserByID(id) {
        $.ajax({
            url: "@ST.GetTrendsFile("BaseConfiguration/GetUserListByID")",
            type: "GET",
            async: true,
            data: { userid: id },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (result) {
                var data = eval("[" + result + "]");
                var d = data[0];
                $("#hidid").val(id);
                $("#loginname").val(d["loginName"]);
                $("#pwd").val(d["loginpwd"]);
                $("#username").val(d["Username"]);
                var leftmenuidData = d["leftmenuids"];
                var weixinidData = d["weixinids"];
                var menuidData = d["menuids"];
                var operateidData = d["operates"];
                setCheckBoxValue(leftmenuidData, "leftmenuid");
                setCheckBoxValue(weixinidData, "weixinid");
                setCheckBoxValue(menuidData, "menuid");
                setOperateListCheckBoxValue(operateidData, "operateid");
                $("#ip").val(d["IPaddress"]);
                $("#drpAccount").val(d["csid"]);
                $("#ckIsValid").attr("checked", d["isvalid"] == "1" ? true : false);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }

    //编辑操作
    function operate(op, id, loginname, pwd, username, leftmenuid, weixinid, menuid, operateid, ip, csid, isvalid) {
        $.ajax({
            url: "@ST.GetTrendsFile("BaseConfiguration/UserUpdate")",
            type: "POST",
            async: true,
            data: { op: op, id: id, loginname: loginname, pwd: pwd, username: username, leftmenuid: leftmenuid, weixinid: weixinid, menuid: menuid, operateid: operateid, ip: ip, csid: csid, isvalid: isvalid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                if (data == "1") {
                    reseting();
                    gotoPage();
                    alert("操作成功。");

                } else alert(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    //重置
    function reseting() {
        $("#hidid").val("0");
        $("#loginname").val("");
        $("#pwd").val("");
        $("#username").val("");
        clearCheckBoxValue("leftmenuid");
        clearCheckBoxValue("weixinid");
        clearCheckBoxValue("menuid");
        //clearCheckBoxValue("operateid");
        //隐藏div
        //$("#divAllOperate").css("display", "none");
        $(".manage_register_p1").css("display", "none");
        //取消div里的checkbox
        clearAllOperateCheckBox();
        $("#ip").val("");
        $("#drpAccount").val("-1");
        $("#ckIsValid").attr("checked", true);
    }

    //翻页
    function PageCountHtml() {
        pageCount = Math.ceil(totalCount / pageSize);
        if (pageCount == 0) {
            pageCount = 1;
        }
        $("#manage_page").html(PageHtml(pageIndex, pageCount));
        $("#manage_page .manage_page_right").click(function () {
            pageIndex = parseInt($("#txtPageNum").val()) + parseInt($(this).attr("tname"));
            $("#manage_page").html(PageHtml(pageIndex, pageCount));
            gotoPage();
        });
        $("#manage_page .tiaozhuan").click(function () {
            var pageNum = $("#txtPageNum").val();
            try {
                pageIndex = parseInt(pageNum);
            } catch (e) {
                alert("请输入数字");
                return false;
            }

            if (pageNum > 0 && pageNum <= pageCount) {
                $("#manage_page").html(PageHtml(pageIndex, pageCount));
                gotoPage();
            }
            else {
                alert("当前数字超出范围");
                return false;
            }
        });
    }
</script>

